package com.sopt.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sopt.friends.ChatActivity;
import com.sopt.friends.R;
import com.sopt.friends.statics.FriendsStaticVars;
import com.sopt.friends.sub.FreshActivity;
import com.sopt.friends.vo.FriendsChat;
import com.sopt.friends.vo.FriendsChatRoom;
import com.sopt.friends.vo.FriendsCommonItem;

public class ChatListAdapter extends BaseAdapter{
	public Context mContext;
	public List<FriendsChat> mMemberList;
	public int mLayout;
	
	public ChatListAdapter(Context context, int layout, List<FriendsChat> inputList) {
		mContext = context;
		mMemberList = inputList;
		mLayout = layout;
	}

	public int getCount() {
		return mMemberList.size();
	}

	public Object getItem(int position) {
		return mMemberList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		
		final FriendsChat mRowItem = mMemberList.get(position);
		LayoutInflater inflate = LayoutInflater.from(mContext);
		View mCurrentView = convertView;
		
		mCurrentView = inflate.inflate(mLayout, parent, false);
		
		TextView mTextViewContent = (TextView)mCurrentView.findViewById(R.id.chatContent);
		
		TextView mTextViewMemberName = (TextView)mCurrentView.findViewById(R.id.chatName);
		TextView mTextViewRegdate = (TextView)mCurrentView.findViewById(R.id.chatRegdate);
				
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		
		if(mRowItem.getMine()!=null && mRowItem.getMine().length()>0){
			if( mTextViewMemberName != null )	mTextViewMemberName.setText( "* Me" );
			if( mTextViewContent != null ){	mTextViewContent.setText( mRowItem.getContent() );
				mTextViewContent.setTextSize(12);
				LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,0.0F);
				mTextViewContent.setLayoutParams(containerParams);
				mTextViewContent.setGravity(Gravity.RIGHT);
			}
			if( mTextViewRegdate != null )
				try {
					mTextViewRegdate.setText( DateUtils.getRelativeTimeSpanString( sdf.parse(mRowItem.getRegdate()).getTime() ) );
					mTextViewRegdate.setTextSize(12);
				} catch (ParseException e) {
					
				}
		}else{
			if( mTextViewMemberName != null )	mTextViewMemberName.setText( mRowItem.getFrommembernum() );
			if( mTextViewContent != null )	mTextViewContent.setText( mRowItem.getContent() );
			if( mTextViewRegdate != null )
				try {
					mTextViewRegdate.setText( DateUtils.getRelativeTimeSpanString( sdf.parse(mRowItem.getRegdate()).getTime() ) );
				} catch (ParseException e) {
					
				}
		}
		
		return mCurrentView;
	}
}
